package cn.edu.xust.configuration.exception;

import cn.edu.xust.util.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Arrays;

/**
 * 捕获全局异常的处理
 *
 * @author 25758
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandle {

    /**
     * 处理Assert的异常
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public R handler(IllegalArgumentException e) {
        return R.error(400, e.getMessage());
    }

    /**
     * 类型转换异常
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = ClassCastException.class)
    public R handleCast(ClassCastException e) {
        log.error("类型转换异常");
        log.error(e.toString());
        e.printStackTrace();
        return R.error(HttpStatus.BAD_REQUEST.value(), "非法请求，联系管理员");
    }

    /**
     * 运行时错误处理 为了兼容之前的程序 返回值为data2
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = RuntimeException.class)
    public R handle(RuntimeException e) {
        log.info(String.valueOf(e));
        e.printStackTrace();
        return R.error(HttpStatus.BAD_REQUEST.value(), "联系管理员");
    }


    /**
     * 自定义异常200
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = Default200Exception.class)
    public R emu200Exception(Default200Exception e) {
        log.warn("错误代码:" + e.getErrorConstant().getErrorCode() + ":" + e.getErrorConstant().getErrorMsg());
        return R.error(e.getErrorConstant().getErrorCode(), e.getErrorConstant().getErrorMsg()+e.getExtmsg());

    }

    /**
     *
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    public R emu200Exception(MissingServletRequestParameterException e) {
        return R.error(400,e.getMessage());

    }

    /**
     * 自定义异常400
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = Default400Exception.class)
    public R emu400Exception(Default200Exception e) {
        log.error("错误代码:" + e.getErrorConstant().getErrorCode() + ":" + e.getErrorConstant().getErrorMsg());
        return R.error(Integer.parseInt(e.getErrorConstant().getErrorMsg()), e.getErrorConstant().getErrorMsg());
    }


    /**
     * 异常
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = Exception.class)
    public R handle2(Exception e) {
        log.info(Arrays.toString(e.getStackTrace()));
        return R.error(HttpStatus.BAD_REQUEST.value(), "联系管理员");
    }


    /**
     * 无权限异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = UnauthorizedException.class)
    public R handleUnauthorizedException(Exception e) {
        log.warn(String.valueOf(e));
        return R.error(HttpStatus.BAD_REQUEST.value(), "无此权限,请联系管理员获取");
    }

}